<?php
declare(strict_types=1);

namespace Zms\Unit;

use StdClass;

/**
 * 对象工具类
 */
class Obj
{
    /**
     * 对象转数组
     * @param object|array $obj
     * @return array
     */
    public static function toArray($obj): array
    {
        if (gettype($obj) === 'array') {
            return $obj;
        }
        return Json::decode(Json::encode($obj));
    }

    /**
     * @describe 生成一个空对象
     * @user zlf <zms-pro@qq.com> 2025/9/9
     * @param array $data
     * @return StdClass
     */
    public static function create($data = []): StdClass
    {
        $object = new StdClass;
        foreach ($data as $key => $value) {
            $object->{$key} = $value;
        }
        return $object;
    }
}

